package org.umlviewer.model.usecase_diagram;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.umlviewer.model.DiagramElement;
import org.umlviewer.model.Constants;
import org.umlviewer.paint.PaintUtils;

/**
 *
 * @author Rafa
 */
public class Actor extends DiagramElement {
    
    private final int RADIO = 20;
    private final int LEN = 20;
    private int width=0;
    
    public Actor()
    {
        setName("");
    }
    
    public Actor(String name)
    {
        setName(name);                
    }
    
    @Override
    public void paint(Graphics g2) {
        Graphics2D g = (Graphics2D)g2;        
        int x, y;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        this.width = RADIO;
        
        /*if(Needrefresh())
        {
            this.width = getName().length() * Constants.SLETTER + 10;
            setRefresh(false);
        }*/
        
        //circulo
        g.setColor(getLineColor());
        g.drawOval(getPosX(), getPosY(), RADIO, RADIO);
        g.setColor(getFillColor());
        g.fillOval(getPosX()+1, getPosY()+1, RADIO-1, RADIO-1);
        g.setColor(getLineColor());
        setWidth(this.width);
        //linea vertical
        x = getPosX() + RADIO/2;
        y = getPosY() + RADIO;
        g.drawLine(x, y, x, y+LEN);
        //linea horizontal
        g.drawLine(x-RADIO/2, y+5, x+RADIO/2, y+5);
        //pies
        g.drawLine(x, y+LEN, x-RADIO/2, y+LEN+10);
        g.drawLine(x, y+LEN, x+RADIO/2, y+LEN+10);
        //nombre
        g.setColor(Constants.DEFAULTCOLOR);
        g.setFont(Constants.boldFont);
        PaintUtils.drawCenterString(g, getName(), getPosX(), y+LEN+10+15, getWidth());
        //g.drawString(getName(), x - (getName().length()/2)*Constants.SLETTER, y+LEN+10+15);
        g.setFont(Constants.defaultFont);
        //alto del rectangulo
        setHeight(RADIO+LEN+LEN);
        
        if(isSelected())
            super.paintSelected(g);
                
    }
    
    public Element xmiSerialize(){
        Namespace xmins = Constants.XMINAMESPACE;
        Element actor = new Element("ownedMember");
        actor.setAttribute("type","uml:Actor",xmins);
        actor.setAttribute("id",getId(),xmins);
        actor.setAttribute("name",getName());
        actor.setAttribute("visibility","public");
        return actor;
    }

    
}
